#!/bin/bash

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

# 日志函数
log_info() {
    echo -e "${GREEN}[INFO] $1${NC}"
}

log_warn() {
    echo -e "${YELLOW}[WARN] $1${NC}"
}

log_error() {
    echo -e "${RED}[ERROR] $1${NC}"
}

# 检查 Ceph 服务状态
check_ceph_status() {
    if ! ceph status &> /dev/null; then
        log_error "Ceph 服务未运行"
        exit 1
    fi
}

# 检查集群健康状态
check_cluster_health() {
    local health_status=$(ceph health detail)
    if [[ $health_status == *"HEALTH_OK"* ]]; then
        log_info "集群健康状态: 正常"
    else
        log_warn "集群健康状态: 异常"
        echo "$health_status"
    fi
}

# 检查 OSD 状态
check_osd_status() {
    local osd_status=$(ceph osd tree)
    local down_osds=$(echo "$osd_status" | grep "down")
    if [ -n "$down_osds" ]; then
        log_warn "发现离线 OSD:"
        echo "$down_osds"
    else
        log_info "所有 OSD 状态正常"
    fi
}

# 检查存储池状态
check_pool_status() {
    local pools=$(ceph osd pool ls)
    for pool in $pools; do
        local pool_stats=$(ceph osd pool stats $pool)
        if [[ $pool_stats == *"no stats available"* ]]; then
            log_warn "存储池 $pool 状态异常"
        else
            log_info "存储池 $pool 状态正常"
        fi
    done
}

# 检查容量使用情况
check_capacity() {
    local capacity=$(ceph df detail)
    local used_percent=$(echo "$capacity" | grep "RAW USED" | awk '{print $5}' | sed 's/%//')
    
    if [ -n "$used_percent" ]; then
        if [ $(echo "$used_percent > 80" | bc) -eq 1 ]; then
            log_warn "存储容量使用率超过 80%: $used_percent%"
        else
            log_info "存储容量使用率正常: $used_percent%"
        fi
    fi
}

# 检查性能指标
check_performance() {
    # 检查 OSD 性能
    local osd_perf=$(ceph osd perf)
    local slow_osds=$(echo "$osd_perf" | awk '$2 > 100 {print}')
    
    if [ -n "$slow_osds" ]; then
        log_warn "发现性能较慢的 OSD:"
        echo "$slow_osds"
    else
        log_info "OSD 性能正常"
    fi

    # 检查客户端性能
    local client_perf=$(ceph osd pool stats)
    local slow_pools=$(echo "$client_perf" | awk '$2 > 100 {print}')
    
    if [ -n "$slow_pools" ]; then
        log_warn "发现性能较慢的存储池:"
        echo "$slow_pools"
    else
        log_info "存储池性能正常"
    fi
}

# 检查网络状态
check_network() {
    local network_status=$(ceph osd ping)
    local slow_nodes=$(echo "$network_status" | awk '$2 > 100 {print}')
    
    if [ -n "$slow_nodes" ]; then
        log_warn "发现网络延迟较高的节点:"
        echo "$slow_nodes"
    else
        log_info "网络状态正常"
    fi
}

# 检查监控节点状态
check_mon_status() {
    local mon_status=$(ceph mon stat)
    local mon_count=$(echo "$mon_status" | grep -o "mons" | wc -l)
    
    if [ "$mon_count" -lt 3 ]; then
        log_warn "监控节点数量不足: $mon_count"
    else
        log_info "监控节点状态正常: $mon_count 个节点"
    fi
}

# 检查 MDS 状态
check_mds_status() {
    local mds_status=$(ceph mds stat)
    if [[ $mds_status == *"up:active"* ]]; then
        log_info "MDS 状态正常"
    else
        log_warn "MDS 状态异常:"
        echo "$mds_status"
    fi
}

# 检查 RGW 状态
check_rgw_status() {
    local rgw_status=$(ceph -s | grep "rgw")
    if [ -n "$rgw_status" ]; then
        log_info "RGW 状态正常"
    else
        log_warn "RGW 状态异常"
    fi
}

# 生成报告
generate_report() {
    local report_file="/var/log/ceph/monitor_report_$(date +%Y%m%d_%H%M%S).log"
    
    {
        echo "=== Ceph 集群监控报告 ==="
        echo "时间: $(date)"
        echo "=== 集群健康状态 ==="
        ceph health detail
        echo "=== OSD 状态 ==="
        ceph osd tree
        echo "=== 存储池状态 ==="
        ceph osd pool stats
        echo "=== 容量使用情况 ==="
        ceph df detail
        echo "=== 性能指标 ==="
        ceph osd perf
        echo "=== 网络状态 ==="
        ceph osd ping
        echo "=== 监控节点状态 ==="
        ceph mon stat
        echo "=== MDS 状态 ==="
        ceph mds stat
        echo "=== RGW 状态 ==="
        ceph -s | grep "rgw"
    } > "$report_file"
    
    log_info "监控报告已生成: $report_file"
}

# 发送告警
send_alert() {
    local alert_message="$1"
    local alert_level="$2"
    
    # 这里可以添加发送告警的具体实现
    # 例如：发送邮件、短信、钉钉等
    echo "[$alert_level] $alert_message" >> /var/log/ceph/alerts.log
}

# 主函数
main() {
    # 检查 Ceph 服务状态
    check_ceph_status
    
    # 执行各项检查
    check_cluster_health
    check_osd_status
    check_pool_status
    check_capacity
    check_performance
    check_network
    check_mon_status
    check_mds_status
    check_rgw_status
    
    # 生成报告
    generate_report
}

# 执行主函数
main "$@" 